#encoding:UTF-8
import mylib
import re
import MySQLdb
import pythonlibSB

conn = mylib.create_conn()


def get_licthidau():
    
    try:
        results = get_url_bangxephang()
        
        for r in results:
            
            url,id_giai,url_time = r[0],r[1],r[2]
            datetime_today = mylib.get_date_today()
            content = mylib.get_data(url)
            get_str = pythonlibSB.s_string(content,'class="text-conent"','div')
            if id_giai in (2,3,4,5):
                get_str = pythonlibSB.s_string(get_str,'class="matches"','table')
                
                reg = re.compile(' style=""')
                for match in reg.findall(get_str):
                    get_str = get_str.replace(match,'')
                     
                results = process_table(get_str,'size\\s*=\\s*["]2["][>]([^><]+)[<][/]')
                results = pythonlibSB.chuanHoaHtml(results)
                insert_lichthidau(id_giai, url_time, results, datetime_today)
                
            elif id_giai in (1,7,8):
                get_str = pythonlibSB.s_string(get_str,'id="table3','table')
                
                reg = re.compile(' style=""')
                for match in reg.findall(get_str):
                    get_str = get_str.replace(match,'')
                
                results = process_table(get_str,'size\\s*=\\s*["]2["][>]([^><]+)[<][/]')
                results = pythonlibSB.chuanHoaHtml(results)
                insert_lichthidau(id_giai, url_time, results, datetime_today)
        print "Xong"
    except:
        print "Loi xu ly"
            

def insert_lichthidau(id_giai,url_time,content,content_time):
    try:
        content = str(MySQLdb.escape_string(content))
        cmd = "CALL insert_lichthidau('%s','%s','%s','%s')"%(id_giai,url_time,content,content_time)
        print cmd
        cursor = conn.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute(cmd)
        cursor.close()
        print "insert thanh cong"
    except:
        print "Loi insert"
    


def process_table(in_str,regex):

    last_str = ""
    start = in_str.find('<tr')
    while start != -1:

        end = in_str.find('</tr>',start)
        if end == -1:
            break
        t = in_str[start:end]
        reg  = re.compile(regex)
        x =reg.findall(t)
        if len(x) > 0:
            new_str = "<p>"
            for i in x:
                new_str = new_str+str(i)+" "
            new_str = new_str + "</p>"
            last_str = last_str + new_str
            
        start = end + 1
#    print last_str
    return last_str



def get_url_bangxephang():
    try:
        results = []
        cmd = "SELECT * FROM tbl_lichthidau WHERE `status` = 0;"
        url_results = mylib.get_result_sql(conn, cmd)
        for u in url_results:
            url = u['url_lichthidau']
            id_giai = u['id_giai']
            url_time = u['url_time']
            results.append((url,id_giai,url_time))
        
        return results
    except:
        return []

if __name__ == "__main__":
#    results = get_url_bangxephang()
#    print results
    get_licthidau()